package mianyang.zhangsan.com.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.pojo.Attendance;
import mianyang.zhangsan.com.pojo.AttendanceCustom;
import mianyang.zhangsan.com.pojo.Course;
import mianyang.zhangsan.com.service.AttendanceService;
import mianyang.zhangsan.com.service.CourseService;
import mianyang.zhangsan.com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/attendance")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;

    /**
     * 考勤列表查询
     * @param page
     * @param rows
     * @param studentId
     * @param courseId
     * @param type
     * @param date
     * @return
     */
    @RequestMapping(value = "/list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Object list(
            @RequestParam(value = "page", defaultValue = "1")int page,
            @RequestParam(value = "rows", defaultValue = "10")int rows,
            @RequestParam(value = "studentId", defaultValue = "0")int studentId,
            @RequestParam(value = "courseId", defaultValue = "0")int courseId, String type, String date){
        Page<Attendance> attendancePage = attendanceService.queryByPage(page, rows, studentId, courseId, type, date);
        List<AttendanceCustom> attendances = new ArrayList<>();
        AttendanceCustom custom = new AttendanceCustom();
        for (Attendance attendance:attendancePage.getRecords()){
            String courseName = courseService.queryNameById(attendance.getCourseId());
            String studentName = studentService.queryUserNameById(attendance.getStudentId());
            custom = attendanceService.attendancePackage(attendance, courseName, studentName);
            attendances.add(custom);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", attendancePage.getPages());
        map.put("rows", attendances);
        return map;
    }

    /**
     * 添加考勤信息
     * @param attendance
     * @return
     */
    @RequestMapping(value = "/add", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String add(Attendance attendance){
        if (attendance != null){
            attendanceService.addAttendance(attendance);
            return "{\"success\": true, \"message\": \"添加成功\"}";
        }
        return "{\"success\": false}";
    }

    /**
     * 删除考勤信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(int id){
        if (id != 0){
            attendanceService.deleteAttendance(id);
            return "{\"success\": true, \"message\": \"删除成功\"}";
        }
        return "{\"success\": false}";
    }
}
